//
//  waKuangViewController.m
//  gpos_app
//
//  Created by apple on 2018/8/20.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "waKuangViewController.h"
#import "Masonry.h"
#import "waKuangViewCell.h"
#import "GPosAPI.h"
#import "SVProgressHUD.h"
#import "homeModel.h"
#import  "minerModel.h"
#import "XMRegisterViewController.h"
@interface waKuangViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UIImageView * topImageView;
@property (nonatomic,strong)UILabel * kjtsLabel;
@property (nonatomic,strong)UITableView * tableView;
@property (nonatomic,strong)UIButton * popButton;
@property (nonatomic,strong)NSMutableArray *modelArr;
@property (nonatomic,strong)NSMutableArray * minerArr;

@end

@implementation waKuangViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:244/255.0f green:244/255.0f blue:244/255.0f alpha:1];
    self.view.userInteractionEnabled = YES;
    self.navigationController.navigationBar.hidden = YES;
    [self loadData];
    
}

- (void)loadData{
    int user = [[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] intValue];
    NSDictionary *params = @{@"userId" : @(user)};
    [GPosAPI requestGetUserMinerWithParams:params andCallback:^(id obj) {
        NSLog(@"obj---%@",obj);
        if ( obj == nil ){
            [SVProgressHUD showInfoWithStatus:@"请求失败"];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
            [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
            
        }else {
            self.modelArr = [[NSMutableArray alloc] init];
            self.minerArr = [[NSMutableArray alloc] init];
            if ([[obj objectForKey:@"code"] intValue] == 200) {
                NSArray * arr = [[NSArray alloc] init];
                arr = [obj objectForKey:@"data"];
                for (NSDictionary * dic in arr) {
                    homeModel *model = [[homeModel alloc] init];
                    model.stageName = [dic objectForKey:@"stageName"];
                    model.dailyIncome = [dic objectForKey:@"dailyIncome"];
                    model.Number = [dic objectForKey:@"number"];
                    if ([[dic objectForKey:@"number"] intValue]>0) {
                        for (int i=0; i<[[dic objectForKey:@"number"] intValue]; i++) {
                            minerModel * mode = [minerModel new];
                            mode.stageName = [dic objectForKey:@"stageName"];
                            mode.dailyIncome = [dic objectForKey:@"dailyIncome"];
                            [self.minerArr addObject:mode];
                        }
                        
                    }
                    [self.modelArr addObject:model];
                }
                
            }else if ([[obj objectForKey:@"code"] intValue] == 401 || [[obj objectForKey:@"code"] intValue] == 402) {
                UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
                //storyboard里的ID
                XMRegisterViewController *vc = [mainStory instantiateInitialViewController];
                vc.navigationController.navigationBarHidden=NO;
                //                    //设置当前窗口的根控制器
                [UIApplication sharedApplication].keyWindow.rootViewController=vc;
                
                [SVProgressHUD showInfoWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                
            }else {
                
                [SVProgressHUD showErrorWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                
                
            }
        }
        [self topImageLoadView];
    }];
    
    
}
- (void)topImageLoadView {
    
    self.topImageView = [[UIImageView alloc] init];
    self.topImageView.image = [UIImage imageNamed:@"banner_kj"];
    self.topImageView.userInteractionEnabled = YES;
    [self.view addSubview:self.topImageView];
    
    self.popButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.popButton setImage:[UIImage imageNamed:@"icon_fanhui_b"] forState:UIControlStateNormal];
    [self.topImageView addSubview:self.popButton];
    
    
    self.kjtsLabel = [[UILabel alloc] init];
    self.kjtsLabel.textColor = [UIColor whiteColor];
    self.kjtsLabel.font = [UIFont systemFontOfSize:16];
    [self.topImageView addSubview:self.kjtsLabel];
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.layer.cornerRadius = 8;
    self.tableView.layer.masksToBounds = YES;
    [self.view addSubview:self.tableView];
    
    UIView * footerView = [[UIView alloc] init];
    footerView.backgroundColor = [UIColor colorWithRed:244/255.0f green:244/255.0f blue:244/255.0f alpha:1];
    self.tableView.tableFooterView = footerView;
    if (IS_IPHONE5){
        [self.topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).with.offset(0);
            make.top.equalTo(self.view).with.offset(0);
            make.right.equalTo(self.view).with.offset(0);
            make.height.equalTo(@200);
        }];
        
        [self.popButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.topImageView).with.offset(25);
            make.left.equalTo(self.topImageView).with.offset(10);
        }];
        
        [self.kjtsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.topImageView);
            make.bottom.equalTo(self.topImageView.mas_bottom).with.offset(-38);
            
        }];
        
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).with.offset(15);
            make.top.equalTo(self.topImageView.mas_bottom).with.offset(-18);
            make.right.equalTo(self.view).with.offset(-15);
            make.bottom.equalTo(self.view).with.offset(-10);
        }];
        
        
    }else {
        [self.topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).with.offset(0);
            make.top.equalTo(self.view).with.offset(0);
            make.right.equalTo(self.view).with.offset(0);
            make.height.equalTo(@262);
            
        }];
        
        [self.popButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.topImageView).with.offset(40);
            make.left.equalTo(self.topImageView).with.offset(10);
        }];
        
        [self.kjtsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.topImageView);
            make.bottom.equalTo(self.topImageView.mas_bottom).with.offset(-51);
            
        }];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).with.offset(15);
            make.top.equalTo(self.topImageView.mas_bottom).with.offset(-28);
            make.right.equalTo(self.view).with.offset(-15);
            make.bottom.equalTo(self.view).with.offset(-kSystemNavHeight);
        }];
        
    }
    
    [self.popButton  addTarget:self action:@selector(popButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.kjtsLabel.text = [NSString stringWithFormat:@"共有%@台GPOS智能正在计算",self.number];
    if ([self.number intValue] == 0) {
//        self.footerView.hidden = YES;
        self.tableView.hidden = YES;
        UIImageView *questView = [[UIImageView alloc] init];
        questView.image = [UIImage imageNamed:@"icon_kuangji"];
        [self.view addSubview:questView];
        [questView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.topImageView.mas_bottom).with.offset(50);
            make.centerX.equalTo(self.view);
        }];
        UILabel *questLabel = [[UILabel alloc] init];
        questLabel.text = @"暂无GPOS智能";
        questLabel.textColor = [UIColor grayColor];
        questLabel.font = [UIFont systemFontOfSize:15];
        [self.view addSubview:questLabel];
        [questLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(questView.mas_bottom).with.offset(10);
            make.centerX.equalTo(self.view);
        }];
    }
    
    
    
}

- (void)popButtonAction:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.minerArr.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//rankingCell
    
    
    //            UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    //            if (!cell) {
    //                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    //            }
    //            cell.textLabel.text = @"测试数据";
    waKuangViewCell * cell = [[waKuangViewCell alloc] init];
    if (indexPath.section == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"wakuangCell"];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"waKuangViewCell" owner:self options:nil] firstObject];
        }
        
    }
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"wakuang" ofType:@"gif"];
    NSData *gifData = [NSData dataWithContentsOfFile:path];
    cell.wedVIew.scalesPageToFit = YES;
    [cell.wedVIew loadData:gifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
    cell.wedVIew.backgroundColor = [UIColor clearColor];
    cell.wedVIew.opaque = NO;
    
    cell.kjbianhaoLabel.text = [NSString stringWithFormat:@"%ld号GPOS智能",indexPath.row+1];
    cell.kjjieduanLabel.text = [self.minerArr[indexPath.row] stageName];
//    NSNumber * nsNumber1 = @([[self.minerArr[indexPath.row] dailyIncome] floatValue]);
    cell.kjshouyiLabel.text = [NSString stringWithFormat:@"%@GPOS",[self.minerArr[indexPath.row] dailyIncome]];;
    //
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (IS_IPHONE5) {
        return 100;
    }else {
        
        return 130;
    }
    
    return 0;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed=YES;
    }
    return self;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
